-- This file is part of the Fantasy Grounds Open Foundation Ruleset project. 
-- For the latest information, see http://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.


function hide()
	setVisible(false);
	window[trigger[1]].setVisible(true);
end

function onEnter()
	hide();
end

function onLoseFocus()
	hide();
end

function onValueChanged()
	-- The target value is a series of consecutive window lists or sub windows
	local targetnesting = {};
	
	for w in string.gmatch(target[1], "(%w+)") do
		table.insert(targetnesting, w);
	end

	local target = window[targetnesting[1]];
	applyTo(target, targetnesting, 2);

	window[trigger[1]].updateWidget(getValue() ~= "");
end

function applyTo(target, nesting, index)
	local targettype = type(target);
	
	if targettype == "windowlist" then
		if index > #nesting then
			target.applyFilter();
			return;
		end
		
		for k, w in pairs(target.getWindows()) do
			applyTo(w[nesting[index]], nesting, index+1);
		end
	elseif targettype == "subwindow" then
		applyTo(target.subwindow[nesting[index]], nesting, index+1);
	end
end
